
package com.csc440.team2.smil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import org.xmlpull.v1.XmlSerializer;


import android.os.Environment;
import android.util.Log;
import android.util.Xml;


public class SMILWriter
{
    public static boolean writeMessage(String message)
    {
        // create a new file called "new.xml" in the SD card
        File newxmlfile = new File(Environment.getExternalStorageDirectory() + "/testsmil.xml");
        try
        {
            newxmlfile.createNewFile();
        }
        catch (IOException e)
        {
            Log.e("IOException", "exception in createNewFile() method");
            return false;
        }
        // we have to bind the new file with a FileOutputStream
        FileOutputStream fileos = null;
        try
        {
            fileos = new FileOutputStream(newxmlfile);
        }
        catch (FileNotFoundException e)
        {
            Log.e("FileNotFoundException", "can't create FileOutputStream");
            return false;
        }
        // we create a XmlSerializer in order to write xml data
        XmlSerializer serializer = Xml.newSerializer();
        try
        {
            // we set the FileOutputStream as output for the serializer, using
            // UTF-8 encoding
            serializer.setOutput(fileos, "UTF-8");
            // Write <?xml declaration with encoding (if encoding not null) and
            // standalone flag (if standalone not null)
            serializer.startDocument(null, Boolean.valueOf(true));
            // set indentation option
            serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
            // start a tag called "root"
            serializer.startTag(null, "smil");
            // i indent code just to have a view similar to xml-tree
            serializer.startTag(null, "body");

            serializer.startTag(null, "seq");
            // set an attribute called "attribute" with a "value" for <child2>
            serializer.attribute(null, "duration", "5");
            serializer.text(message);
            serializer.endTag(null, "seq");

            serializer.endTag(null, "body");

            serializer.endTag(null, "smil");
            serializer.endDocument();
            // write xml data into the FileOutputStream
            serializer.flush();
            // finally we close the file stream
            fileos.close();
        }
        catch (IOException ioe)
        {
            Log.e("IOException", "Error serializing XML", ioe);
            return false;
        }

        return true;
    }
}
